package com.yznote.cloudesk.message.controller;

import com.yznote.cloudesk.common.Anno.CollectLogs;
import com.yznote.cloudesk.common.constants.ModuleConstant;
import com.yznote.cloudesk.common.pojo.*;
import com.yznote.cloudesk.common.utils.YDUtils;
import com.yznote.cloudesk.message.service.RoomService;
import com.yznote.cloudesk.websocket.server.YDWebSocketSession;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.temporal.TemporalAccessor;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/room")
@CollectLogs(moduleName = ModuleConstant.ROOM_MODULE)
public class RoomController {
    @Autowired
    RoomService roomService;

    @ApiOperation("创建房间")
    @PostMapping("/createRoom")
    public Result creatRoom(@RequestBody Room room) {
        Room res = roomService.creatRoom(room);
        if (res != null) return Result.success(res);
        else return Result.fail("创建房间失败");
    }

    @ApiOperation("/selectUsersInRoom")
    public Result selectUsersInRoom(@RequestParam("roomId") Long roomId) {
        List<User> users = roomService.selectUsersInRoom(roomId);
        if (users != null) Result.success(users);
        return Result.fail("查询房间里的用户失败");
    }

    @ApiOperation("查詢用戶的房間")
    @GetMapping("/getRoomList")
    public Result getRoomList() {
        List<Room> roomList = roomService.queryRoomList();
        if (roomList == null) return Result.fail("查询房间列表失败");
        return Result.success(roomList);
    }

    @ApiOperation("/请求加入房间")
    @GetMapping("/joinRoom")
    public Result joinTheRoom(@RequestParam("roomId") Long roomId) {
        Boolean res = roomService.joinTheRoom(roomId);
        if (res) return Result.success("申请加入房间成功");
        else return Result.fail("申请失败");
    }

    @ApiOperation("查询房间")
    @GetMapping("/searchRoom")
    public Result searchRoom(@RequestParam("roomName") String roomName, @RequestParam(value = "pageSize", required = false) Integer pageSize, @RequestParam(value = "pageNum", required = false) Integer pageNum) {
        List<Room> roomList = roomService.queryRoomListByName(roomName);
        return YDUtils.toPage(pageNum, pageSize, roomList);
    }

    @ApiOperation("邀请好友加入房间")
    @GetMapping("/inviteUserToRoom")
    public Result inviteUserToRoom(@RequestParam("roomId") Long roomId, @RequestParam("userId") Long userId) {
        Boolean res = roomService.inviteUserToRoom(roomId, userId);
        if (res) {
            return Result.success("邀请好友成功");
        } else return Result.fail("邀请好友失败");
    }

    @ApiOperation("查询房间邀请")
    @GetMapping("/queryInvite")
    public Result queryInvite() {
        List<Room> rooms = roomService.queryInvite();
        return Result.success(rooms);
    }

    @ApiOperation("查询房间的申请")
    @GetMapping("/queryApply")
    public Result queryApply() {
        List<RoomUser> roomUsers = roomService.queryApply();
        return Result.success(roomUsers);
    }

    @ApiOperation("查询指定房间的申请")
    @GetMapping("/queryApplyByRoomId")
    public Result queryApplyByRoomId(@RequestParam("roomId") Long roomId) {
        List<RoomUser> roomUsers = roomService.queryApplyByRoomId(roomId);
        return Result.success(roomUsers);
    }

    @ApiOperation("接受房间邀请")
    @GetMapping("/acceptRoom")
    public Result acceptRoom(@RequestParam("roomId") Long roomId) {
        Boolean res = roomService.acceptRoom(roomId);
        if (res) return Result.success("加入房间成功");
        return Result.fail("加入房间失败");
    }

    @ApiOperation("拒绝房间邀请")
    @GetMapping("/rejectRoom")
    public Result rejectRoom(@RequestParam("roomId") Long roomId) {
        Boolean res = roomService.rejectRoom(roomId);
        if (res) return Result.success("你已拒绝邀请");
        return Result.fail("拒绝邀请失败");
    }

    @ApiOperation("拉取最新的10条消息")
    @GetMapping("/getMessages")
    public Result getMessage(@RequestParam("roomId") Long roomId, @RequestParam Integer pageSize, @RequestParam Integer pageNum) {
        List<Message> messageList = roomService.getMessages(roomId);
        return YDUtils.toPage(pageNum, pageSize, messageList);
    }

    @ApiOperation("拉取离线消息条数")
    @GetMapping("/getOfflineMessageCount")
    public Result getOfflineMessageCount(@RequestParam Long roomId) {
        //List<Message> messageList = roomService.getOfflineMessage(roomId);
        Integer offlineMessageCount = roomService.getOfflineMessageCount(roomId);
        return Result.success(offlineMessageCount);
    }

    @ApiOperation("拉取离线消息")
    @GetMapping("/getOfflineMessage")
    public Result getOfflineMessage(@RequestParam Long roomId, @RequestParam("pageSize") Integer pageSize, @RequestParam("pageNum") Integer pageNum) {
        List<Message> messageList = roomService.getOfflineMessage(roomId);
        return YDUtils.toPage(pageNum, pageSize, messageList);
    }

    @ApiOperation("拉取用户的被邀请消息")
    @GetMapping("/getInviteNotice")
    public Result getInviteNotice() {
        List<Room> roomList = roomService.getInviteNotice();
        return Result.success(roomList);
    }

    @ApiOperation("获取用户管理的房间")
    @GetMapping("/getOwnerRoomList")
    public Result getOwnerRoomList() {
        List<Room> roomList = roomService.getOwnerRoomList();
        return Result.success(roomList);
    }

    @ApiOperation("拉取房间最新消息的接口")
    @GetMapping("/getLastMessage")
    public Result getLastMessage(@RequestParam("roomId") Long roomId) {
        Message message = roomService.getLastMessage(roomId);
        return Result.success(message);
    }

    @ApiOperation("修改拉取消息的最新时间戳")
    @GetMapping("/pushLastTimestamp")
    public Result pushLastTimestamp(@RequestParam("time") Long timestamp, @RequestParam("roomId") Long roomId) {
        LocalDateTime time = LocalDateTime.ofInstant(Instant.ofEpochMilli(timestamp), ZoneOffset.of("+8"));
        Boolean res = roomService.pushLastTimestamp(time, roomId);
        if (res) return Result.success("修改已读时间成功");
        else return Result.fail("修改已读时间失败");
    }

    @ApiOperation("解散房间")
    @GetMapping("/dismissRoom")
    public Result removeRoom(@RequestParam("roomId") Long roomId) {
        Boolean res = roomService.dismissRoom(roomId);
        if (res) return Result.success("解散房间成功");
        return Result.fail("解散房间失败");
    }

    @ApiOperation("群聊上传头像")
    @PostMapping("/pushRoomHeaders")
    public Result pushRoomHeaders(@RequestParam MultipartFile mtf, @RequestParam Long roomId) throws Exception {
        roomService.pushUserHeaders(mtf, roomId);
        return Result.success(null);
    }

    @ApiOperation("房主踢人")
    @GetMapping("/removeUser")
    public Result removeUser(@RequestParam Long otherUserId, @RequestParam Long roomId) {
        roomService.removeUser(otherUserId, roomId);
        return Result.success(null);
    }

    @ApiOperation("申请加入房间by roomName")
    @GetMapping("/tryJoinRoomByRoomname")
    public Result tryJoinRoomByRoomname(@RequestParam String roomName) {
        roomService.tryJoinRoomByRoomname(roomName);
        return Result.success(null);
    }

    @ApiOperation("申请加入房间by roomId")
    @GetMapping("/tryJoinRoomByRoomId")
    public Result tryJoinRoomByRoomId(@RequestParam Long roomId) {
        roomService.tryJoinRoomByRoomId(roomId);
        return Result.success(null);
    }

    /**
     * 方法废弃
     *
     * @param roomId
     * @return
     */
    @Deprecated
    public Result joinRoom(@RequestParam("roomId") Long roomId) {
        Boolean res = roomService.joinRoom(roomId);
        if (res) {
            return Result.success("加入房间成功");
        } else {
            return Result.fail("加入房间失败");
        }
    }

}
